package school.community.controller;

import lombok.extern.slf4j.Slf4j;
import school.community.cache.HotTagCache;
import school.community.dto.PaginationDTO;
import school.community.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@Slf4j
public class IndexController {

  @Autowired
  private InfoService infoService;

  @Autowired
  private HotTagCache hotTagCache;

  @GetMapping("/")
  public String index(Model model,
                      @RequestParam(name = "page", defaultValue = "1") Integer page,
                      @RequestParam(name = "size", defaultValue = "10") Integer size,
                      @RequestParam(name = "search", required = false) String search,
                      @RequestParam(name = "tag", required = false) String tag,
                      @RequestParam(name = "sort", required = false) String sort) {
    PaginationDTO pagination = infoService.list(search, tag, sort, page, size);
    List<String> tags = hotTagCache.getHots();
//    log.info("tags {}", tags);
    model.addAttribute("pagination", pagination);
    model.addAttribute("search", search);
    model.addAttribute("tag", tag);
    model.addAttribute("tags", tags);
    model.addAttribute("sort", sort);
    return "index";
  }
}
